﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmProduct : XtraForm
    {
        public FrmProduct()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmProduct_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            LoadProductType();
            LoadProduct(0);
        }
        /// <summary>
        /// 加载产品类型
        /// </summary>
        private void LoadProductType()
        {
            var bll = new DMZS.BLL.ProductType();
            var ds = bll.GetListForUIExpand(string.Empty);
            tlsProductType.DataSource = ds.Tables[0];
        }
        /// <summary>
        /// 加载产品
        /// </summary>
        /// <param name="ProductTypeID"></param>
        private void LoadProduct(long ProductTypeID)
        {
            if (tlsProductType.FocusedNode != null)
            {
                ProductTypeID = Convert.ToInt64(tlsProductType.FocusedNode["ID"]);
            }
            else
            {
                ProductTypeID = 0;
            }
            var bll = new DMZS.BLL.Product();
            var ds = bll.GetListForUIExpand(ProductTypeID, string.Empty);
            if (ds != null && ds.Tables.Count > 0)
            {
                grdProduct.DataSource = ds.Tables[0];
            }
        }

        private void tlsProductType_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tlsProductType.FocusedNode != null)
            {
                LoadProduct(Convert.ToInt64(tlsProductType.FocusedNode["ID"]));
            }
        }
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmProduct_Editor();
            frm.ID = 0;
            frm.ShowDialog();
        }
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvProduct.GetFocusedDataRow() != null)
            {
                var frm = new FrmProduct_Editor();
                frm.ID = Convert.ToInt64(grvProduct.GetFocusedDataRow()["ID"]);
                frm.ShowDialog();
            }
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvProduct.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                var bll = new DMZS.BLL.Product();

                if (bll.DeleteExpand(Convert.ToInt64(grvProduct.GetFocusedDataRow()["ID"])))
                {
                    if (tlsProductType.FocusedNode != null)
                    {
                        LoadProduct(Convert.ToInt64(tlsProductType.FocusedNode["ID"]));
                    }
                }
                else
                {
                    XtraMessageBox.Show("该产品已被引用,无法删除。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvProduct.IndicatorWidth = grvProduct.IndicatorWidth == -1 ? 40 : grvProduct.IndicatorWidth;
            grvProduct.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvProduct);
            CommonControl.DxGridHelper.InitGridStyle(tlsProductType);
        }

        private void grvProduct_DoubleClick(object sender, EventArgs e)
        {
            if (grvProduct.GetFocusedDataRow() != null)
            {
                var frm = new FrmProduct_Editor();
                frm.ID = Convert.ToInt64(grvProduct.GetFocusedDataRow()["ID"]);
                frm.ShowDialog();
            }
        }

        private void grvProduct_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }
    }
}
